<script lang="ts" setup>
import { ref, nextTick, reactive, watch } from "vue"
import { RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
// import {  type FormInstance } from "element-plus"
import { companygetUndeliveredClientApi } from "@/api/conventional"

const dialogVisible = ref<boolean>(false)
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const handleClose = (done: () => void) => {
  done()
}
// const chartRef = ref<any>(null)
const dialogShow = async () => {
  dialogVisible.value = true
  getTableData()
  nextTick(() => {
    console.log(11)
  })
}
defineExpose({ dialogShow })

//#region 查
const tableData = ref<any[]>([])
// const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  stars: 1
})
const getTableData = () => {
  loading.value = true
  companygetUndeliveredClientApi({
    pageNum: paginationData.currentPage,
    pageSize: paginationData.pageSize,
    ...searchData
  })
    .then((res) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
// const handleSearch = () => {
//   paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
// }
// const resetSearch = () => {
//   searchFormRef.value?.resetFields()
//   handleSearch()
// }
//#endregion
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="列表"
    width="80%"
    :before-close="handleClose"
    draggable
    destroy-on-close
    fullscreen
  >
    <el-card class="box-card" v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <!-- <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增</el-button> -->
        </div>
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="company_name" label="公司名称" align="center" />
          <el-table-column prop="user_text" label="业务员" align="center" />
          <el-table-column prop="company_type_text" label="客户类别" align="center" />
          <el-table-column prop="invoicing_type_text" label="是否开票" align="center" />
          <el-table-column prop="check_type_text" label="先款先票" align="center" />
          <el-table-column prop="trade_date" label="交易时间" align="center" />
          <el-table-column prop="create_time" label="创建时间" align="center" />
          <!-- <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-dialog>
</template>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
</style>
